﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Mvc;
using System.Data;
using System.Security.Principal;
using System.Web.Security;
using LeadVinaglue.Models;
using LeadVinaglue.Helpers;
using LeadVinaglue.Utility;

namespace LeadVinaglue.Repository
{
    public class BaseManagementRepository : Controller
    {
        // Create an instance of entity
        LeadVinaglueEntities entities = new LeadVinaglueEntities();

        public MenuModel GetMenuItems(string roleName, string pageType)
        {
            MenuModel model = new MenuModel();
            string fileName = "";
            if (roleName == "SuperAdministrator")
                fileName = "Configs/MenuAdmin.xml";
            else
                fileName = "Configs/MenuUser.xml";

            string xmlPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
            model = (MenuModel)XmlHelper.DeSerializeObject(xmlPath, typeof(MenuModel));
            model.MenuList.ForEach(x => x.CurrentMenu = false);

            if (!string.IsNullOrEmpty(pageType))
            {
                switch (pageType)
                {
                    case "Leads":
                        model.MenuList.Single(x => x.TabName.Contains("Leads")).CurrentMenu = true;
                        break;
                    case "Dashboard":
                        model.MenuList.Single(x => x.TabName.Contains("Dashboard")).CurrentMenu = true;
                        break;
                    case "Menu":
                        model.MenuList.Single(x => x.TabName.Contains("Menu")).CurrentMenu = true;
                        break;
                }
            }

            return model;
        }

        public HeadModel GetHeader()
        {
            HeadModel model = new HeadModel();

            model.LinkText = "Thoát";
            model.LinkUrl = "/Account/LogOff";
            model.IsAuthenticated = true;

            if (User != null && User.Identity != null)
            {
                var user = new LeadVinaglue.Repository.UserRepository().GetSingle(User.Identity.Name);
                model.EmailAddress = user.Email;
            }
            else
            {
                model.EmailAddress = "linhtv83@gmail.com";
            }

            return model;
        }

        public FileResult CreateCSVFile(DataTable dt, string fileName)
        {
            MemoryStream output = new MemoryStream();
            StreamWriter sw = new StreamWriter(output, System.Text.Encoding.UTF8);

            // First we will write the headers.
            int iColCount = dt.Columns.Count;
            for (int i = 0; i < iColCount; i++)
            {
                sw.Write(dt.Columns[i]);

                if (i < iColCount - 1)
                {
                    sw.Write(",");
                }
            }
            sw.Write(sw.NewLine);
            // Now write all the rows.
            foreach (DataRow dr in dt.Rows)
            {
                for (int i = 0; i < iColCount; i++)
                {
                    if (!Convert.IsDBNull(dr[i]))
                    {
                        string value = dr[i].ToString().Replace("\"", "\"\"");
                        sw.Write("\"" + value + "\"");
                    }
                    if (i < iColCount - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);
            }
            sw.Flush();
            output.Seek(0, SeekOrigin.Begin);

            return File(output, "text/csv", fileName + "_" + DateTime.Now.ToString("MM-dd-yyyy-hhmmsstt") + ".csv");
        }

    }
}